﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CollaborateTools;
using System.Drawing;
using ATEC.ATDataCoreNET;
using SSDPNET;
using SSDPNET.Geom;
using SSDPNET.DataCore;
using ATEC.GeometryNET;
using ATEC.ATDataCoreNET.ATObjects;
using SSDPNET.ATObjects;

namespace ProjectTreeExample
{
    /// <summary>
    /// 工程树节点附加的数据，用户自定义
    /// </summary>
    public class NodeTagData
    {
       public ObjectId id { get; set; }
       public ATScene scene { get; set; }
    }

    public class CreateProjectTree
    {
        public static GlobalManagementForm gForm = GlobalManagementForm.Instance;
        public static TreeView m_treeView = gForm.CurrentTreeView;
        public static MenuItem m_MenuItem = new MenuItem("通用");//通用-功能
        public static MenuItem m_LocateMenuItem = new MenuItem("定位");//定位功能

        public static MenuItem m_1stNodeMenuItem = new MenuItem("一级节点功能");//一级节点功能
        public static MenuItem m_2ndNodeMenuItem = new MenuItem("二级节点功能"); //二级节点功能
        public static MenuItem m_3rdNodeMenuItem = new MenuItem("三级节点功能");//三级节点功能
        //获取当前激活Scene
        public static ATSceneRef scene = ScenesFunctions.SceneRefGetActive();

        public CreateProjectTree()
        {
            //初始化事件绑定
            InitCommand();
            InitMenuItem();
        }

        public void CreateProjectTreeExample()
        {
            //清空工程节点
            //没有-->创建
            //有-->清空
            gForm.ClearDbChildNodes(GNodeType.Tunnel, "示例");

            //找到工程的根节点，更新节点
            TreeNode pRoot = gForm.GetDbNode(GNodeType.Tunnel, "示例");
            CreateTreeNode(ref pRoot);

            gForm.TryRefreshTreeView();
            gForm.ExpandNodeByGNodeType(GNodeType.Tunnel);
        }

        public NodeTagData CreateNodeTag()
        {
            NodeTagData tagData = new NodeTagData();

            CurveSet curve = CurveSet.CreateDisk(XEllipse3d.FromCenterRadiusXY(XPoint3d.Zero, 200), CurveSet.BoundaryType.Outer);
            //添加到视口
            ATEC.ATDataCoreNET.ATObjects.ATObject curveSetObj =
ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(scene.GetATScene(), curve, null);

            curveSetObj.AddToScene();

            tagData.id = curveSetObj.ObjectId;
            tagData.scene = curveSetObj.ATScene;

            return tagData;
        }
        
        /// <summary>
        /// 构造节点
        /// </summary>
        /// <param name="treeView"></param>
        public void CreateTreeNode(ref TreeNode pRoot)
        {
            TreeNode node1st = new TreeNode("一级节点");
            TreeNode node2nd = new TreeNode("二级节点");
            TreeNode node3rd = new TreeNode("三级节点");
            {
                //构造一个 需要被管理的数据
                NodeTagData data = CreateNodeTag();
                node3rd.Tag = data;
            }
            node2nd.Nodes.Add(node3rd);
            node1st.Nodes.Add(node2nd);
            pRoot.Nodes.Add(node1st);
        }

        /// <summary>
        /// 初始化全局管理窗体事件
        /// </summary>
        public void InitCommand()
        {
            GlobalManagementForm.TreeNodeMouseClick += TreeViewAfterSelect;
        }
        private void UnInitCommand()
        {
            GlobalManagementForm.TreeNodeMouseClick -= TreeViewAfterSelect;
        }

        /// <summary>
        /// 初始化右键菜单
        /// </summary>
        public void InitMenuItem()
        {
            //menu
            m_MenuItem.Click += M_MenuItem_Click;
            m_LocateMenuItem.Click += M_LocateATObject_Click;
            m_1stNodeMenuItem.Click += M_1stNodeMenuItem_Click;
            m_2ndNodeMenuItem.Click += M_2ndNodeMenuItem_Click;
            m_3rdNodeMenuItem.Click += M_3rdNodeMenuItem_Click;
        }
        private void UnInitMenuItem()
        {
            //menu
            m_MenuItem.Click -= M_MenuItem_Click;
            m_LocateMenuItem.Click -= M_LocateATObject_Click;
            m_1stNodeMenuItem.Click -= M_1stNodeMenuItem_Click;
            m_2ndNodeMenuItem.Click -= M_2ndNodeMenuItem_Click;
            m_3rdNodeMenuItem.Click -= M_3rdNodeMenuItem_Click;
        }

        public void Dispose()
        {
            UnInitCommand();
            UnInitMenuItem();
        }

        /// <summary>
        /// 右键选中节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeViewAfterSelect(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeView tv = (TreeView)(sender);
                if (tv != null)
                {
                    m_treeView = tv;
                    m_treeView.SelectedNode = e.Node;
                    AddMenu(e);
                }
            }
        }

        /// <summary>
        /// 添加右键菜单
        /// </summary>
        /// <param name="e"></param>
        public void AddMenu(TreeNodeMouseClickEventArgs e)
        {
            ContextMenu contextMenu = new ContextMenu();

            TreeNode nodeData = e.Node;
            if (nodeData == null)
                return;

            contextMenu.MenuItems.Add(m_MenuItem);//通用

            if (nodeData.Text.Contains("一"))
            {
                contextMenu.MenuItems.Add(m_1stNodeMenuItem);//一级节点功能
            }
            else if (nodeData.Text.Contains("二"))
            {
                contextMenu.MenuItems.Add(m_2ndNodeMenuItem);//二级节点功能
            }
            else if (nodeData.Text.Contains("三"))
            {
                contextMenu.MenuItems.Add(m_LocateMenuItem);//定位
                contextMenu.MenuItems.Add(m_3rdNodeMenuItem);//三级节点功能
            }
            contextMenu.Show(m_treeView, e.Location);

        }

        #region 右键菜单事件
        private void M_3rdNodeMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("三级节点功能");
        }

        private void M_LocateATObject_Click(object sender, EventArgs e)
        {
            TreeNode node = m_treeView.SelectedNode;
            if (node == null) return;

            NodeTagData data = node.Tag as NodeTagData;
            if (data == null) return;

            //根据id和scene找到 选中节点挂接的对象
            ATObject selectObj = null;
            ObjectId id = data.id;
            ATScene sceneObj = data.scene;

            //从当前的scene中拿到所有的object
            SceneObjectsCollection objCollection = sceneObj.GetGraphicObjects();
            foreach (var obj in objCollection)
            {
                if (obj.ObjectId == id)
                {
                    selectObj = obj;
                }
            }

            if (selectObj == null) return;

            //高亮
            SelectionSetManager.AddObject(selectObj, sceneObj);

            XRange3d range = new XRange3d();
            selectObj.CalcObjectRange(out range, XTransform3d.Identity);

            SSDPNET.IndexedViewSet viewSet = SSDPNET.IViewManager.GetActiveViewSet();
            ATEC.ATDataCoreNET.IndexedViewport viewport = viewSet.GetSelectedViewport();

            // 将当前视口范围调整到scen所在的空间范围
            if (viewport.ChangeAreaFromFit(range.Low, range.High, false, true) != ATEC.ATDataCoreNET.StatusInt.Success)
            {
                return;
            }

            viewport.CenterScrollBar();
            viewport.ForceHealImmediate(500);
        }

        private void M_2ndNodeMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("二级节点功能");
        }

        private void M_1stNodeMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("一级节点功能");
        }

        private void M_MenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("通用右键功能");
        }
        #endregion
    }
}
